<?php
require_once '../sub/init.php';
header("Cache-control: private");
require_once 'chkUadm.php';
if(!in_array('shop',$QXARR))exit(noauth());
if ( !ifint($tg_uid) )alert_adm("ID号不正确","back");
$k=2;
$cid  = intval($tg_uid);
$rowc = $db->ROW(__TBL_TG_USER__,"title","id=".$cid);
if ($rowc)$cname= dataIO($rowc[0],'out');
if ($submitok == 'ajax_addupdate' || $submitok == 'ajax_modupdate'){
	if (empty($title) )json_exit(array('flag'=>0,'msg'=>'请输入分类名称','focus'=>'title'));
	if (str_len($title) >200)json_exit(array('flag'=>0,'msg'=>'亲，分类名称【'.$title.'】这么长有意义么？ 请不要超过20字节','focus'=>'title'));
	$title = dataIO($title,'in',200);
	AddLog('【商家/机构】->商品分类发布修改【商家id:'.$tg_uid.'】');
}
switch ($submitok){
	case "ajax_addupdate":
		$db->query("INSERT INTO ".__TBL_TG_PRODUCT_KIND__." (tg_uid,title,px) VALUES ($tg_uid,'$title',".ADDTIME.")");	
		json_exit(array('flag'=>1));
	break;
	case "ajax_modupdate":
		if(!ifint($id))json_exit(array('flag'=>0,'msg'=>'forbidden'));
		$db->query("UPDATE ".__TBL_TG_PRODUCT_KIND__." SET title='$title' WHERE id=".$id);
		$db->query("UPDATE ".__TBL_TG_PRODUCT__." SET kindtitle='$title' WHERE kind=".$id);
		json_exit(array('flag'=>1));
	break;
	case "ajax_delupdate":
		if(!ifint($id))json_exit(array('flag'=>0,'msg'=>'forbidden'));
		//$db->query("DELETE FROM ".__TBL_TG_PRODUCT__." WHERE kind=".$id);
		$db->query("DELETE FROM ".__TBL_TG_PRODUCT_KIND__." WHERE id=".$id);
		json_exit(array('flag'=>1,'msg'=>'删除成功'));
	break;
	case"ding":
		if (!ifint($id))alert_adm("forbidden","-1");
		$db->query("UPDATE ".__TBL_TG_PRODUCT_KIND__." SET px=".ADDTIME." WHERE id=".$id);
		header("Location: ".SELF."?tg_uid=".$tg_uid);
	break;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link href="css/main.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
<script src="<?php echo HOST;?>/res/www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<style>.box{min-width:800px}</style>
</head>
<body<?php echo (!empty($submitok))?' class="body"':'';?>>
<!--ADD-->
<?php if ($submitok == "add") {?>
	<table class="table W90_ Mtop50">
	<form id="ZEAIFORM" name="ZEAIFORM" method="post" >
	<tr>
	<td class="tdL">分类名称</td>
	<td class="tdR"><input id="title" name="title" type="text" class="W200 size2" size="30" maxlength="200"></td>
	</tr>
	<tr>
	  <td class="tdL">&nbsp;</td>
	  <td class="tdR">
	    <input name="submitok" type="hidden" value="ajax_addupdate">
        <input name="tg_uid" type="hidden" value="<?php echo $tg_uid;?>">
        <input name="k" type="hidden" value="<?php echo $k;?>">
	    <button type="button" id="save" class="btn size3">确定并保存</button>
      </td>
	  </tr>
	</form>
	</table>
<!--MOD-->
<?php
}else if($submitok == "mod"){
	$row = $db->ROW(__TBL_TG_PRODUCT_KIND__,"title","tg_uid=".$tg_uid." AND id=".$id,"name");
	if ($row){
		$title = dataIO($row['title'],'out');
	}else{exit('forbidden');}
	?>
	<table class="table W90_ Mtop20">
    <form name="ZEAIFORM" id="ZEAIFORM" method="post" enctype="multipart/form-data">
    <tr>
    <td class="tdL">分类名称</td>
    <td class="tdR"><input name="title" type="text" class="W200 size2" id="title" size="30" maxlength="100" value="<?php echo $title;?>"></td>
    </tr>
    <tr>
    <td class="tdL">&nbsp;</td>
    <td class="tdR">
        <input name="submitok" type="hidden" value="ajax_modupdate">
        <input name="id" type="hidden" value="<?php echo $id;?>">
        <input name="tg_uid" type="hidden" value="<?php echo $tg_uid;?>">
        <input name="k" type="hidden" value="<?php echo $k;?>">
    <button type="button" id="save" class="btn size3">确定并保存</button>
    </td>
    </tr>
    </form>
    </table>
<!--LIST-->
<?php }
if ($submitok == "add" || $submitok == "mod") {?>
<script>
	save.onclick = function(){
		zeai.ajax({url:'TG_u_product_kind'+zeai.extname,form:ZEAIFORM},function(e){var rs=zeai.jsoneval(e);
			if (rs.flag == 1){
				parent.location.reload(true);
			}else if(rs.flag == 0){
				parent.zeai.msg(rs.msg,o(rs.focus));
			}else{
				parent.zeai.msg(rs.msg);
			}		
		});
	}
</script>
<?php exit;}?>
<div class="box">
	<div class="boxL">
        <a href="TG_u_mod.php?<?php echo "tg_uid=$tg_uid&f=$f&g=$g&k=$k&p=$p&ifback=1"; ?>&submitok=mod">商家资料</a>
        <a href="TG_u_product.php?<?php echo "tg_uid=$tg_uid&k=2"; ?>">商品管理<?php echo '<b>'.$db->COUNT(__TBL_TG_PRODUCT__,"tg_uid=".$tg_uid).'</b>';?></a>
        <a href="<?php echo "TG_u_product_kind.php?tg_uid=$tg_uid&k=2"; ?>" class="ed">商品分类<?php echo '<b>'.$db->COUNT(__TBL_TG_PRODUCT_KIND__,"tg_uid=".$tg_uid).'</b>';?></a>
	</div>
	<div class="boxR">
    	<div class="topsearch" style="padding-bottom:21px"><?php echo $cname.'【ID:'.$tg_uid.'】';?></div>
        <div class="topsearch" style="padding-bottom:10px">
    		<button type="button" class="btn tips" onClick="zeai.iframe('新增分类','<?php echo SELF;?>?submitok=add&tg_uid=<?php echo $tg_uid;?>',500,300)" ><i class="ico addico">&#xe622;</i>新增分类</button>
        </div>
        <!---->
        <?php
		$rt = $db->query("SELECT id,title FROM ".__TBL_TG_PRODUCT_KIND__." WHERE tg_uid=".$tg_uid." ORDER BY px DESC,id DESC");
		$total = $db->num_rows($rt);
		if ($total <= 0 ) {
			if ($submitok !== "add")echo "<div class='nodataico'><i></i>暂无分类<br><a class='btn size2' onClick=\"zeai.iframe('新增分类','".SELF."?submitok=add&tg_uid=".$tg_uid."',500,300)\">新增分类</a></div>";
		} else {
			$page_skin = 2;$pagesize=20;require_once ZEAI.'sub/page.php';?>
			<table class="tablelist">
			<tr>
				<th width="60" align="center">ID</th>
				<th width="60" align="center">置顶</th>
				<th width="150">分类名称</th>
				<th width="120">商品数量</th>
				<th align="center">&nbsp;</th>
				<th width="60" class="center">修改</th>
				<th width="60" class="center">删除</th>
				<th width="25" class="center">&nbsp;</th>
			</tr>
			<?php
			for($i=1;$i<=$pagesize;$i++) {
				$rows = $db->fetch_array($rt);
				if(!$rows) break;
				$id = $rows['id'];
				$title = dataIO($rows['title'],'out');
				$wznum = $db->COUNT(__TBL_TG_PRODUCT__,"kind=".$id." AND tg_uid=".$tg_uid);
			?>
			<tr>
			<td width="60" height="40" align="center"><?php echo $id;?></td>
			<td width="60" height="40" align="center"><a href="<?php echo SELF."?id=".$id; ?>&submitok=ding&tg_uid=<?php echo $tg_uid;?>" class="topico" title="置顶"></a></td>
			<td width="150" class="S14"><?php echo $title;?></td>
			<td width="120" class="S14"><a href="TG_u_product.php?kind=<?php echo $id;?>&tg_uid=<?php echo $tg_uid;?>" class="aHUI"><?php echo($wznum>0)?'<font class="Cf00 S14">'.$wznum.'</font>':0;?></a></td>
			<td align="center">&nbsp;</td>
			<td width="60" class="center"><a value="<?php echo $id;?>" class="editbtn" title="<?php echo $title;?>" onClick="zeai.iframe('修改【<?php echo $title;?>】分类','<?php echo SELF;?>?submitok=mod&id=<?php echo $id;?>&tg_uid=<?php echo $tg_uid;?>&k=2',500,300)"></a></td>
			<td width="60" class="center"><a value="<?php echo $id; ?>" wznum="<?php echo $wznum;?>" class="delbtn" title="<?php echo $title;?>"></a></td>
			<td width="25" class="center">&nbsp;</td>
			</tr>
			<?php } ?>
			<?php if ($total > $pagesize){?>
			<tfoot><tr>
			<td colspan="8" class="Pleft10"><?php echo '<div class="pagebox FR">'.$pagelist.'</div>'; ?></td>
			</tr></tfoot>
			<?php } ?>
			</table>
			<script>
			zeai.listEach('.delico',function(obj){
				var id = parseInt(obj.getAttribute("value"));
				var wznum = parseInt(obj.getAttribute("wznum"));
				var title = obj.getAttribute("title");
				var tips = (wznum>0)?'当前分类包含 '+wznum+' 条信息':'';
				obj.onclick = function(){
					zeai.confirm('<font color="red">请慎重！</font><br>'+tips+'<br>真的要删除【'+title+'】么？<br>删除后不可恢复',function(){
						zeai.ajax('TG_u_product_kind'+zeai.ajxext+'submitok=ajax_delupdate&id='+id+"&tg_uid=<?php echo $tg_uid;?>",function(e){var rs=zeai.jsoneval(e);
							if (rs.flag == 1){location.reload(true);}else{zeai.msg(rs.msg);}
						});
					});
				}
			});
			</script>
	  <?php } ?>
        <!---->
	</div>
    <div class="clear"></div>
</div>
<?php require_once 'bottomadm.php';?>
